//
//  XGWelcomeViewController.swift
//  WeIBo
//
//  Created by 苏文潇 on 2017/3/8.
//  Copyright © 2017年 苏文潇. All rights reserved.
//

import UIKit
import SDWebImage

class XGWelcomeViewController: UIViewController {

//    MARK:懒加载控件
    
    //    MARK:01 欢迎视图
    private lazy var backGroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //    MARK:02 头像
    private lazy var userImage: UIImageView = {
       
        let userImage = UIImageView()
//        userImage.image = UIImage(named: "avatar_default_big")
        
        if let imageUrl = XGUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large {
             userImage.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
//        设置圆角
        userImage.layer.cornerRadius = 45
        userImage.layer.masksToBounds = true
        
        return userImage
    }()
    
    //    MARK:03 设置底部label
    private lazy var textLabel: UILabel = {
    
        let label = UILabel()
        
        if let name = XGTokenUserAccount.loadUserAccount()?.name {
            label.text = "欢迎回来 \(name)"
        }else {
            label.text = "欢迎回来"
        }
        
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.orange
        label.textAlignment = .center
        label.alpha = 0
        
        return label
    
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUI()
    }
    
//    MARK: 重写loadView 将默认的view替换成欢迎页面
    override func loadView() {
        view = backGroundImageView
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        
        startUserImage()
        
    }
    
//    MARK:实现头像的动画
    
    private func startUserImage()
    {
        userImage.snp_updateConstraints { (make) in
            make.top.equalTo(100)
        }
        
        UIView.animate(withDuration: 1, delay: 0.5, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: UIViewAnimationOptions(rawValue: 0), animations: {
//        强制改变位置，否则的话设置的位置就不会发生改变
            self.view.layoutIfNeeded()
            
        }, completion: {(_) -> Void in
            UIView.animate(withDuration: 0.5, animations: {
            
                self.textLabel.alpha = 1
                
            }, completion: { (_) in
//                切换到主页控制器视图
//                let vc = XGTabBarViewController()
//                UIApplication.shared.keyWindow?.rootViewController = vc
                  NotificationCenter.default.post(name: NSNotification.Name(SwitchNotification), object: nil)
            })
            
        })
    }
    
    
    //    MARK:创建UI
    private func setUI()
    {
        view.addSubview(userImage)
        view.addSubview(textLabel)
        
        
        userImage.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(300)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        textLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImage.snp_bottom).offset(10)
            make.centerX.equalTo(userImage)
        }
    }

}
